from ApiTestEngine.core2.cases import run_test
from django.http import JsonResponse
from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet, GenericViewSet
from rest_framework import permissions, mixins

from TestInterface.models import TestInterfaceModel, TestInterfaceCaseModel
from TestInterface.serializers import TestInterfaceSerializer, TestInterfaceCaseSerializer, \
    TestInterfaceCaseListSerializer, TestInterfaceCaseGetSerializer, TestInterfaceCSerializer
from Testproject.models import TestEnvModel


class TestInterfaceView(mixins.CreateModelMixin,
                        mixins.UpdateModelMixin,
                        mixins.DestroyModelMixin,
                        mixins.ListModelMixin,
                        GenericViewSet):
    queryset = TestInterfaceModel.objects.all()
    serializer_class = TestInterfaceSerializer
    permission_classes = [permissions.IsAuthenticated]
    filterset_fields = ['project']

    def get_serializer_class(self):
        if self.action == "list":
            return TestInterfaceCSerializer
        else:
            return self.serializer_class

    def run_case(self, request):
        env_id = request.data.get("env")
        cases = request.data.get("cases")
        if not env_id:
            return Response({"error": "env参数不能为空"}, status=400)
        if not cases:
            return Response({"error": "cases参数不能为空"}, status=400)
        env = TestEnvModel.objects.get(id=env_id)
        env_config = {
            "ENV": {
                "host": env.host,
                "headers": env.headers,
                **env.global_variable,
                **env.debug_global_variable
            },
            "DB": env.db,
            "global_func": env.global_func
        }
        cases_datas = [
            {
                'name': "调试运行",
                'Cases': [cases]
            }
        ]
        r, ENV = run_test(cases_datas, env_config)
        env.debug_global_variable = ENV
        env.save()
        return JsonResponse(r)


class TestInterfaceCaseView(ModelViewSet):
    queryset = TestInterfaceCaseModel.objects.all()
    serializer_class = TestInterfaceCaseSerializer
    permission_classes = [permissions.IsAuthenticated]
    filterset_fields = ['interface']

    # 获取列表
    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())

        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = TestInterfaceCaseListSerializer(page, many=True)
            return self.get_paginated_response(serializer.data)

        serializer = TestInterfaceCaseListSerializer(queryset, many=True)
        return Response(serializer.data)

    # 获取interface详细信息
    def retrieve(self, request, *args, **kwargs):
        instance = self.get_object()
        serializer = TestInterfaceCaseGetSerializer(instance)
        return Response(serializer.data)

    # def get_serializer_class(self):
    #     if self.action == "list":
    #         return TestInterfaceCaseListSerializer
    #     elif self.action == "retrieve":
    #         return TestInterfaceCaseGetSerializer
    #     else:
    #         return self.serializer_class
